字符串匹配算法---Brute force、KMP、Sunday

字符串匹配---输入原字符串(String)和子串(又称模式),输出为子串在原字符串中的首次出现的位置。在这儿,我们介绍用于字符串匹配的Brute force、KMP和Sunday方法。

1.Brute force

该方法又称暴力搜索。

匹配时间复杂度O(N*M)

主要过程:从原字符串开始搜索,若出现不能匹配,则从下一个位置继续。

int bf(const char *text, const char *find)  
{  
    if (text == '/0' || find == '/0')  
        return -1;  
    int find_len = strlen(find);  
    int text_len = strlen(text);  
    if (text_len < find_len)  
        return -1;  
    char *s = text;  
    char *p = s;  
    char *q = find;  
    while (*p != '/0')  
    {  
        if (*p == *q)  
        {  
            p++;  
            q++;  
        }  
        else  
        {  
            s++;  
            p = s;  
            q = find;  
        }  
        if (*q == '/0')  
        {  
            return (p - text) - (q - find);  
        }  
    }  
    return -1;  
}  

2.KMP

匹配时间复杂度:O(M+N)

主要过程:通过对字串进行预处理,找到next数组(子串首尾重合长度数组);当发现不能匹配时,可以不进行回溯。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100

void cal_next( char * str, int * next, int len )
{
    int i, j;

    next[0] = -1;
    for( i = 1; i < len; i++ )
    {
        j = next[ i - 1 ];
        while( str[ j + 1 ] != str[ i ] && ( j >= 0 ) )
        {
            j = next[ j ];
        }
        if( str[ i ] == str[ j + 1 ] )
        {
            next[ i ] = j + 1;
        }
        else
        {
            next[ i ] = -1;
        }
    }
}

int KMP( char * str, int slen, char * ptr, int plen, int * next )
{
    int s_i = 0, p_i = 0;

    while( s_i < slen && p_i < plen )
    {
        if( str[ s_i ] == ptr[ p_i ] )
        {
            s_i++;
            p_i++;
        }
        else
        {
            if( p_i == 0 )
            {
                s_i++;
            }
            else
            {
                p_i = next[ p_i - 1 ] + 1;
            }
        }
    }
    return ( p_i == plen ) ? ( s_i - plen ) : -1;
}

int main()
{
    char str[ N ] = {0};
    char ptr[ N ] = {0};
    int slen, plen;
    int next[ N ];

    while( scanf( "%s%s", str, ptr ) )
    {
        slen = strlen( str );
        plen = strlen( ptr );
        cal_next( ptr, next, plen );
        printf( "%d\n", KMP( str, slen, ptr, plen, next ) );
    }
    return 0;
}

3.Sunday

匹配时间复杂度O(N*M)

主要过程:从尾部出发,判断原串字符在目标串中是否存在,根据判断结果,可以一次往后移动较大偏移量。

int in_dst(const char word, const char *dst)
 {
     int i = strlen(dst) - 1;

     while (dst[i] != '\0')
     {
         if (dst[i] == word)
           return (strlen(dst) - i);
        i--;
     }
     return -1;
}
 
 
int sunday(const char *src, const char *dst)
{
     int SrcLenth = strlen(src), DstLenth = strlen(dst);
     int j = 0, i = 0, pos;
 
     while (j < SrcLenth)
     {
         i = 0;
         while (src[j+i] == dst [i])	i++;
         if (dst[i] == '\0')   printf("%d~%d\n", j, j+DstLenth-1);  
 
         if ((pos = in_dst(src[j+DstLenth], dst)) != -1) j += pos;
         else j += (DstLenth + 1); 
     }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值